package edu.unh.sustain;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;
import android.widget.Toast;

public class TripPlannerActivity extends Activity
{
	/* holds the current content of the leave/arrive spinner */
	private String leave;
	private String leaveDate;
	private String leaveTime;
	private String[] options;
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.tripplanner);
	    
	    options = getResources().getStringArray(R.array.plantripspinner);
	    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
	            this, R.array.plantripspinner, android.R.layout.simple_spinner_item);
	    Spinner spinner = (Spinner) findViewById(R.id.leave);
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
	    
	    final Calendar c = Calendar.getInstance();
	    DatePicker dp = (DatePicker) findViewById(R.id.leavedate);
	    dp.init(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH), new MyOnDateChangedListener());
	    leaveDate = new StringBuilder().append(c.get(Calendar.MONTH)).append("/").append(c.get(Calendar.DAY_OF_MONTH)).append("/").append(c.get(Calendar.YEAR)).toString();
	    leaveTime = new StringBuilder().append(c.get(Calendar.HOUR)).append(":").append(c.get(Calendar.MINUTE)).toString();
	    
	    TimePicker tp = (TimePicker) findViewById(R.id.leavetime);
	    tp.setOnTimeChangedListener(new MyOnTimeChangedListener());
	    
	    Button planit = (Button) findViewById(R.id.planit);
	    planit.setOnClickListener(new MyOnClickListener());
	}
	
	/**
	 * 
	 * @author blake
	 *
	 */
	public class MyOnClickListener implements View.OnClickListener
	{
		public void onClick(View view)
		{
			Intent i = new Intent(view.getContext(), ShowPlanActivity.class);
			
			String pointa = ((EditText)findViewById(R.id.pointa)).getText().toString();
			String pointb = ((EditText)findViewById(R.id.pointb)).getText().toString();
	    	
			if(pointa.equals("") || pointb.equals(""))
			{
				Toast.makeText(getApplicationContext(), "All fields are required", 1000).show();
			}
			else
			{
				i.putExtra("pointa", pointa);
				i.putExtra("pointb", pointb);
				i.putExtra("leave", leave);
				i.putExtra("leaveDate", leaveDate);
				i.putExtra("leaveTime", leaveTime);
				
	            startActivityForResult(i, 0);
			}
		}
	}
	
	/**
	 * 
	 * @author blake
	 *
	 */
	public class MyOnItemSelectedListener implements OnItemSelectedListener
	{		
	    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
	    {
	    	leave = options[pos];
	    }

	    public void onNothingSelected(AdapterView<?> parent)
	    {
	    	// Do nothing.
	    }
	}
	
	/**
	 * 
	 * @author blake
	 *
	 */
	public class MyOnDateChangedListener implements OnDateChangedListener
	{
		public void onDateChanged(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) 
		{
			leaveDate = (++monthOfYear) + "/" + dayOfMonth + "/" + year;
		}
	}
	
	/**
	 * 
	 * @author blake
	 *
	 */
	public class MyOnTimeChangedListener implements OnTimeChangedListener
	{
		public void onTimeChanged(TimePicker view, int hour, int minute)
		{
			String meridium = "am";
			String minuteStr = new Integer(minute).toString();
			
			if(hour >= 12)
			{
				hour -= 12;
				meridium = "pm";
			}
			
			if(hour == 0)
			{
				hour = 12;
			}
			
			if (minute < 10)
				minuteStr = "0" + minute;
			
			leaveTime = new StringBuilder().append(hour).append(":").append(minuteStr).append(meridium).toString();
		}
	}
}
